676A - Nicholas and Permutation - CodeForces Solution


constructive algorithms implementation *800

Please click on ads to support us..

Python Code:

n = int(input())
lst = list(map(int, input().split()))

ma = max(lst)
mi = min(lst)

m = max([lst.index(ma), lst.index(mi), n -lst.index(ma)-1, n-lst.index(mi)-1])
print(m)

C++ Code:

#include<bits/stdc++.h>
using namespace std;
#define endl "\n"
const long long MOD=1000000007;
#define yes cout<<"YES\n";
#define no cout<<"NO\n"
#define pi acos(-1)
typedef long long ll;
typedef vector<ll> vec;
typedef vector<string> vecs;
typedef pair<ll, ll> pll;
typedef vector<pair<ll, ll>> vp;
typedef set<ll> st;
typedef set<pair<ll, ll>> sp;
typedef multiset<ll> mst;
typedef multiset<pair<ll, ll>> mstp;
typedef map<ll, ll> mp;
typedef map<char, ll> mpc;
#define all(a) (a).begin(),(a).end()
#define rall(a) (a).rbegin(),(a).rend()
#define sz(x) (int)x.size()
#define sqr(a) ((a) * (a))
#define optimize() ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define fraction() cout.unsetf(ios::floatfield); cout.precision(10); cout.setf(ios::fixed,ios::floatfield);
#define file() freopen("input.txt","r",stdin);freopen("output.txt","w",stdout);
#define dbg(args...) do {cerr << #args << " : "; faltu(args); } while(0)
template < typename T, typename ... hello>void faltu( T arg, const hello &... rest)
{
    cerr << arg << ' ';
    faltu(rest...);
}
//inline void normal(ll &a) { a %= MOD; (a < 0) && (a += MOD); }
//inline ll modMul(ll a, ll b) { a %= MOD, b %= MOD; normal(a), normal(b); return (a*b)%MOD; }
//inline ll modAdd(ll a, ll b) { a %= MOD, b %= MOD; normal(a), normal(b); return (a+b)%MOD; }
//inline ll modSub(ll a, ll b) { a %= MOD, b %= MOD; normal(a), normal(b); a -= b; normal(a); return a; }
//inline ll modPow(ll b, ll p) { ll r = 1; while(p) { if(p&1) r = modMul(r, b); b = modMul(b, b); p >>= 1; } return r; }
//inline ll modInverse(ll a) { return modPow(a, MOD-2); }
//inline ll modDiv(ll a, ll b) { return modMul(a, modInverse(b)); }
// arr n-1 last and 1 start compare previous arr[i+1]>arr[i] min num of moves to make a to b b-(a%b)

ll getmodex(ll a, ll  b)
{
    if(b==0) return 1;
    if(b%2==0)
    {
        auto t=getmodex(a,b/2)%MOD;
        return (t*t)%MOD;
    }
    return(a * getmodex(a,b-1)%MOD)%MOD;
}
//--------------------------------- START ------------------------------------------//
void solve()
{

    ll n;
    cin>>n;
    ll arr[n];
    ll idx=0,idx1=0;
    for(ll i=1; i<=n; i++)
    {
        cin>>arr[i];
        if(arr[i]==1)
        {
            idx1=i;
        }
        if(arr[i]==n)
        {
            idx=i;
        }
    }
    ll ans=max(abs(idx1-n),abs(idx1-1));
    ll ans1=max(abs(idx-n),abs(idx-1));
    cout<<max(ans1,ans)<<endl;

}


int main()
{
    //optimize();
    int t;
//    cin>>t;
//    while(t--)
//    {
//        solve();
//
//    }
    solve();
    // main();
    //for(int i=1;i<=t;i++) cout<<"Case "<<i<<": ",solve();ll n;

    //ll t;scanf("%lld",&t);for(int i=1;i<=t;i++)printf("Case %lld: ",i),solve();
    return 0;

}
//#include<bits/stdc++.h>
//using namespace std;
//#define ll long long
//void rangemin(ll *arr, ll l,ll r)
//{
//    vector<pair<ll,ll>>vec;
//    ll mino=LONG_MAX;
//
//    for(ll i=l; i<=r; i++)
//    {
//        mino=min(mino,arr[i]);
//        vec.push_back({mino,i});
//
//    }
//    ll maxno=LONG_MIN;
//    for(auto x:vec)
//    {
//        maxno=max(maxno,x.second);
//    }
//    cout<<mino<<endl;
//    cout<<maxno<<endl;
//
//}
//int main()
//{
//
//    ll n,q;
//    cin>>n>>q;
//    ll arr[n];
//    ll mino=LONG_MAX;
//    for(ll i=1; i<=n; i++)
//    {
//        cin>>arr[i];
//    }
//    while(q--)
//    {
//
//        ll l,r;
//        cin>>l>>r;
//        rangemin(arr,l,r);
//
//    }
//
//}


Comments

Submit
0 Comments
More Questions

1237A - Balanced Rating Changes
1616A - Integer Diversity
1627B - Not Sitting
1663C - Pōja Verdon
1497A - Meximization
1633B - Minority
688B - Lovely Palindromes
66B - Petya and Countryside
1557B - Moamen and k-subarrays
540A - Combination Lock
1553C - Penalty
1474E - What Is It
1335B - Construct the String
1004B - Sonya and Exhibition
1397A - Juggling Letters
985C - Liebig's Barrels
115A - Party
746B - Decoding
1424G - Years
1663A - Who Tested
1073B - Vasya and Books
195B - After Training
455A - Boredom
1099A - Snowball
1651D - Nearest Excluded Points
599A - Patrick and Shopping
237A - Free Cash
1615B - And It's Non-Zero
1619E - MEX and Increments
34B - Sale